// water.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include "list.c"
int A, B, C;


int goal(State s);
List findnexts(State s);
void printpath(List path);
int solve(State s, List path, List passed);

int main(int argc, char* argv[])
{

    State start;
    List path, passed;

    path = CreateList();
    passed = CreateList();
    printf("A:\n");
    scanf("%d",&A);
    printf("B:\n");
    scanf("%d",&B);
    printf("C:\n");
    scanf("%d",&C);

    start.a = 0;
    start.b = 0;

    if (! solve(start, path, passed))
        printf("sorry, no solution\n");
    return 0;
}

int solve(State s, List path, List passed)
{
    List nexts;
    State next;
    Position p;

    p = Last(passed);
    Insert(s, passed, p);//Insert(_________, passed, p);
    p = Last(path);
    Insert(s, path, p);//Insert(_________, path, p);
    if(goal(s))//if (________)
    {
        PrintList(path);
        return 1;//_________;
    }

    nexts = findnexts(s);

    p = First(nexts);//p = ____________;
    while (p)
    {
        next = Retrieve(p);
        if(!Find(next, passed))//if (!Find(next, _______))
            if(solve(next, path, passed))//if (_______________)
                return 1;
        p = Advance(p);
    }
    p = FindPrevious(s,path);//p = FindPrevious(s, ________);
    Delete(path, p);//Delete(_________, p);
    DeleteList(nexts);
    return 0;
}

int goal(State s)
{
    return (s.a == C) || (s.b == C) || (s.b + s.a == C);
}

List findnexts(State s)
{
    State tmp;
    List nexts;
    Position p;

    nexts = CreateList();
    p = Header(nexts);

    //full A
    if (s.a != A)
    {
        tmp.a = A;//tmp.a = _______;
        tmp.b = s.b;
        Insert(tmp, nexts, p);
    }

    //full B
    if (s.b != B)
    {
        tmp.b = B;//tmp.b = ________;
        tmp.a = s.a;
        Insert(tmp, nexts, p);
    }

    //empty A
    if (s.a)
    {
        tmp.a = 0;//tmp.a = _______;
        tmp.b = s.b;
        Insert(tmp, nexts, p);
    }

    //empty B
    if (s.b)
    {
        tmp.b = 0;//tmp.b = _______;
        tmp.a = s.a;
        Insert(tmp, nexts, p);
    }

    //A to B
    if (s.a && (s.b != B))
    {
        int d = B - s.b;
        if (s.a >= d)
        {
            //full B from A
            tmp.a = s.a - d;//tmp.a = _______;
            tmp.b = B;
        }
        else
        {
            //empty A by B
            tmp.a = 0;//tmp.a = _________;
            tmp.b = s.b + s.a;
        }
        Insert(tmp, nexts, p);
    }

    //B to A
    if (s.b && (s.a != A))
    {
        int d = A - s.a;
        if (s.b >= d)
        {
            //full A from B
            tmp.b = s.b - d;
            tmp.a = A;
        }
        else
        {
            //empty B by A
            tmp.b = 0;
            tmp.a = s.a + s.b;//tmp.a = _________;
        }
        Insert(tmp, nexts, p);
    }
    return nexts;
}

